package GUI.Paleta;

import BE.BEPaleta;
import BL.BLCombo;
import BL.BLPaleta;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

public class GUIMantenimientoPaleta extends javax.swing.JDialog {
    private BEPaleta objPaleta;
    private boolean busqueda;

    public BEPaleta getObjPaleta() {
        return objPaleta;
    }

    public void setObjPaleta(BEPaleta objPaleta) {
        this.objPaleta = objPaleta;
    }

    /** Creates new form GUIMantenimientoPallet */
    public GUIMantenimientoPaleta(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.inicializa();
    }

        public GUIMantenimientoPaleta(java.awt.Frame parent, boolean modal, BEPaleta paleta) {
        super(parent, modal);
        initComponents();
        this.inicializa();
        
        objPaleta=paleta;
        busqueda= true;
        if (busqueda) this.modoBusqueda();

        cmbEstado.setSelectedIndex(1);
        cmbEstado.setEnabled(false);
        }

 private void modoBusqueda() {
        btnAgregar.setVisible(false);
        btnEditar.setVisible(false);
        btnEliminar.setText("Aceptar");
        cmbEstado.setSelectedIndex(1);
        cmbEstado.setEnabled(false);
    }
    
    public void inicializa()
    {
        this.setLocationRelativeTo(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
        
        BLCombo blCombo = new BLCombo();
        //Formatos para componentes
        tblBuscarPallet.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        blCombo.setCombo("TIPOPALETA",0,cmbTipo);
        cmbEstado.addItem(new BLCombo(2,"TODOS"));
        cmbEstado.addItem(new BLCombo(1,"ACTIVO"));
        cmbEstado.addItem(new BLCombo(0,"INACTIVO"));
    }

    public void limpiar ()
    {
        BLPaleta blPallet = new BLPaleta();

        blPallet.limpiaTabla(tblBuscarPallet);


         if (!busqueda)
              cmbEstado.setSelectedIndex(0);
        
        cmbTipo.setSelectedIndex(0);
        txtPallet.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlBuscarPallet = new javax.swing.JPanel();
        lblPallet = new javax.swing.JLabel();
        lblEstado = new javax.swing.JLabel();
        txtPallet = new javax.swing.JTextField();
        lblTipoPallet = new javax.swing.JLabel();
        cmbTipo = new javax.swing.JComboBox();
        cmbEstado = new javax.swing.JComboBox();
        btnFiltrar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBuscarPallet = new javax.swing.JTable();
        pnlBotones = new javax.swing.JPanel();
        btnAgregar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Mantenimiento de Paleta");
        setName("GUIMantenimientoPallet"); // NOI18N

        pnlBuscarPallet.setBorder(javax.swing.BorderFactory.createTitledBorder("Buscar Paleta"));

        lblPallet.setText("Paleta:");

        lblEstado.setText("Estado:");

        lblTipoPallet.setText("Tipo de Paleta:");

        btnFiltrar.setText("Filtrar");
        btnFiltrar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnFiltrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFiltrarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBuscarPalletLayout = new javax.swing.GroupLayout(pnlBuscarPallet);
        pnlBuscarPallet.setLayout(pnlBuscarPalletLayout);
        pnlBuscarPalletLayout.setHorizontalGroup(
            pnlBuscarPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarPalletLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPallet)
                    .addComponent(lblEstado))
                .addGap(12, 12, 12)
                .addGroup(pnlBuscarPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmbEstado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtPallet, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlBuscarPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlBuscarPalletLayout.createSequentialGroup()
                        .addComponent(lblTipoPallet)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlBuscarPalletLayout.createSequentialGroup()
                        .addComponent(btnFiltrar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        pnlBuscarPalletLayout.setVerticalGroup(
            pnlBuscarPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarPalletLayout.createSequentialGroup()
                .addGroup(pnlBuscarPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPallet)
                    .addComponent(txtPallet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTipoPallet))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlBuscarPalletLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEstado)
                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnFiltrar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblBuscarPallet.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Paleta", "Tipo de Paleta", "Descripcion", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblBuscarPallet.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(tblBuscarPallet);
        tblBuscarPallet.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        btnAgregar.setText("Agregar");
        btnAgregar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAgregar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar");
        btnEditar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnEditar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnEliminar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBotonesLayout = new javax.swing.GroupLayout(pnlBotones);
        pnlBotones.setLayout(pnlBotonesLayout);
        pnlBotonesLayout.setHorizontalGroup(
            pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBotonesLayout.createSequentialGroup()
                .addContainerGap(28, Short.MAX_VALUE)
                .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        pnlBotonesLayout.setVerticalGroup(
            pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBotonesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pnlBotones, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlBuscarPallet, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlBuscarPallet, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlBotones, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        GUIAgregarPaleta frmAgregarPallet = new GUIAgregarPaleta(null, true);
        frmAgregarPallet.setResizable(false);
        frmAgregarPallet.setVisible(true);

        BLPaleta bLPallet = new BLPaleta();
        bLPallet.limpiaTabla(tblBuscarPallet);
        bLPallet.buscarPallet("",0,0, tblBuscarPallet,0); //PARA BUSCAR TODOS LOS PRODUCTOS
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        int fila;
        int columna = 0; //Columna ID
        int idPallet;

        fila = tblBuscarPallet.getSelectedRow();

        if (fila < 0)
            JOptionPane.showMessageDialog(null,"Debe seleccionar una paleta para editar",
                                          "Información",JOptionPane.INFORMATION_MESSAGE);
        else if (JOptionPane.showConfirmDialog(null,"¿Desea editar la paleta?",
                 "Confirmación",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE) == 0)
        {
            idPallet = Integer.parseInt(tblBuscarPallet.getModel().getValueAt(fila,columna).toString());

            GUIModificarPaleta frmModificarPallet = new GUIModificarPaleta(null,true,idPallet);
            frmModificarPallet.setResizable(false);
            frmModificarPallet.setVisible(true);

            BLPaleta bLPallet = new BLPaleta();
            bLPallet.limpiaTabla(tblBuscarPallet);
            bLPallet.buscarPallet("",0,0, tblBuscarPallet,0); //PARA BUSCAR TODOS LOS PRODUCTOS
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnFiltrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFiltrarActionPerformed
        BLPaleta blPallet = new BLPaleta();
        BLCombo idEstado = (BLCombo)cmbEstado.getSelectedItem();
        BLCombo idTipo = (BLCombo)cmbTipo.getSelectedItem();

        blPallet.buscarPallet(txtPallet.getText(),idTipo.id,
                         idEstado.id,tblBuscarPallet,1); //1 = busqueda por paramtros
    }//GEN-LAST:event_btnFiltrarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        limpiar();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
          if (busqueda)
    {
        BLPaleta objGestorProducto = new BLPaleta();

           objPaleta= objGestorProducto.llenaVentana(Integer.parseInt(tblBuscarPallet.getModel().getValueAt(tblBuscarPallet.getSelectedRow(),0).toString()));



        this.setVisible(false);
    }
          
 else{




        BLPaleta bLPallet = new BLPaleta();
        bLPallet.eliminarPallet(tblBuscarPallet);}
    }//GEN-LAST:event_btnEliminarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GUIMantenimientoPaleta dialog = new GUIMantenimientoPaleta(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnFiltrar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JLabel lblPallet;
    private javax.swing.JLabel lblTipoPallet;
    private javax.swing.JPanel pnlBotones;
    private javax.swing.JPanel pnlBuscarPallet;
    private javax.swing.JTable tblBuscarPallet;
    private javax.swing.JTextField txtPallet;
    // End of variables declaration//GEN-END:variables

}
